
type Method = 'GET' | 'POST'
function request(url: string, method: Method) { }

type Requests = {
  url: string,
  method: Method
}

// 如果没有Request类型的限制，method被推导为了string
const options = {
  url: 'http://www.xxx.com',
  method: 'GET'
}

// 可以断定第二个参数类型是Method
request(options.url, options.method as Method)


const arrayNumber: number[] = [1, 2, 2, 2];
const greaterThan2: number = arrayNumber.find(num => num > 2) as number;


function printLen(message?: string) {
  // !  把undefined和null排除掉
  console.log(message!.length);
}
printLen('sdfsdfsdf')


type NumGenerator = () => number;
function myFunc(numGenerator: NumGenerator | undefined) {
  const num2 = numGenerator!(); // OK
}

let x!: number;
initialize();
console.log(2 * x); // Ok

function initialize() {
  x = 9
}

